//
//  User.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/3/1.
//

#import "User.h"
#import <YYKit.h>
#import "NSDictionary+JSONReadWrite.h"

@implementation User

+ (instancetype)user {
    static User *user = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSDictionary *dict = [NSDictionary JSONWithContentsOfFile:self.localCachePath] ?: @{};
        user = [User modelWithJSON:dict];
    });
    return user;
}

- (NSString *)account {
    //return @"9333555777";
    //return @"9333555778";
    return @"9333555779";
//    return @"9111222333";
}

- (BOOL)synchronize {
    NSDictionary *dict = [self mj_JSONObject];
    return [dict writeToJSONFile:User.localCachePath atomically:YES];
}


+ (NSString *)localCachePath {
    NSString *cachesDirectory = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
    NSFileManager *fm = NSFileManager.defaultManager;
    NSString *filePath = [cachesDirectory stringByAppendingPathComponent:@"api"];
    if (![fm fileExistsAtPath:filePath]) {
        // 创建目录
        NSError *error = nil;
        BOOL success = [fm createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
        NSLog(@"---localCachePath: 目录创建%@: %@", success ? @"成功" : @"失败", success ? filePath : @"");
    } else {
        
    }
    return [filePath stringByAppendingPathComponent:@"user.json"];
}


@end
